home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11934 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.5 KB

  1. Path: pop.gnn.com!HoangTQ
  2. From: Tuyen Hoang <HoangTQ@gnn.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Need Help With CSC Homework
  5. Date: Sat, 16 Mar 1996 22:33:58
  6. Organization: GNN
  7. Message-ID: <4ig14c$h3p@news-e2c.gnn.com>
  8. References: <4g0qun$jp@news.nevada.edu>
  9. NNTP-Posting-Host: www-29-26.gnn.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset="us-ascii"
  12. X-GNN-NewsServer-Posting-Date: 17 Mar 1996 03:32:28 GMT
  13. X-Mailer: GNNmessenger 1.2
  14.  
  15.  
  16. > Troy code:
  17. >pfun.cpp
  18. >
  19. >//*************************************************
  20. >//**             ROMANFUN.CPP                    **
  21. >//*************************************************
  22. >//** This program will add, subtract, multiply,  **
  23. >//** and divide roman numbers. The numbers are   **
  24. >//** assumed to be less than 20 characters long. **
  25. >//** The roman numbers should not be inputed in  **
  26. >//** subtraction form. Examples - IV=6 IIII=4    **
  27. >//*************************************************
  28. >#include <iostream.h>   //for cin and cout
  29. >#include "p.h"      //has typedef and prototypes
  30. >istream& operator>>(istream& in,Romantype& x)
  31. >{
  32. >in>>x.Roman;
  33. >return in;
  34. >}
  35. >
  36. >ostream& operator<<(ostream& out,Romantype x)
  37. >{
  38. >out<<x.Roman;
  39. >return out;
  40. >}
  41. >
  42. >Romantype::Romantype()
  43. >{
  44. >int i;
  45. >for(i=1;i<MAXLENGTH;i++)
  46. >{
  47. >Roman[i]=' ';
  48. >}
  49. >}
  50. >
  51. >//*************************************************
  52. >//**                 ADD FUNCTION                **
  53. >//*************************************************
  54. >//** This function adds two roman numbers. It    **
  55. >//** requires a character array and two roman    **
  56. >//** numbers.                                    **
  57. >//*************************************************
  58. >void Romantype::operator+(Romantype numbertwo)
  59. >{
  60. >int numbero;
  61. >int numbert;
  62. >int answer;
  63. >Romantype answerr;
  64. >numbero=RomanToDecimal(Roman);
  65. >numbert=RomanToDecimal(numbertwo.Roman);
  66. >answer=numbero+numbert;
  67. >DecimalToRoman(answerr.Roman,answer);
  68. >cout<<answerr.Roman;
  69. >}
  70. >
  71. >//*************************************************
  72. >//**     ROMAN TO DECIMAL FUNCTION               **
  73. >//*************************************************
  74. >//** This function converts a roman number to a  **
  75. >//** decimal number. It requires the character   **
  76. >//** array and the number of characters in the   **
  77. >//** array.                                      **
  78. >//*************************************************
  79. >int Romantype::RomanToDecimal(char[MAXLENGTH])
  80. >{
  81. >int number=0;               //value of number intialized to zero
  82. >int i;                      //used in for loop
  83. >for(i=1;i<MAXLENGTH;i++)          //until end of array add values of letters
  84. >{
  85. >switch(Roman[i])
  86. >{
  87. >case 'I':number=number+1;break;
  88. >case 'V':number=number+5;break;
  89. >case 'X':number=number+10;break;
  90. >case 'L':number=number+50;break;
  91. >case 'C':number=number+100;break;
  92. >case 'D':number=number+500;break;
  93. >case 'M':number=number+1000;break;
  94. >}
  95. >}
  96. >return number;    //return decimal value to where the function was called
  97. >}
  98. >
  99. >//*************************************************
  100. >//**     DECIMAL TO ROMAN FUNCTION               **
  101. >//*************************************************
  102. >//** This function converts a decimal number to  **
  103. >//** a roman number. It requires a character     **
  104. >//** array and the value of the decimal number.  **
  105. >//*************************************************
  106. >void Romantype::DecimalToRoman(char[MAXLENGTH]/* ??? */,int answer)
  107. //                                                ^^^what is the name of param. ?
  108. //are you sure what you want to do?
  109. //without the param.'s name  the char array you pass in will be *thrown away*.
  110.  
  111. >{
  112. >int i=1;           //used in for loop
  113. >while(answer-1000>=0) //convert to roman by storing letters in array
  114. >{
  115. >answerr.Roman[i]='M';
  116.   ^^^^  where does the 'answerr'-RomanType object- come from?
  117. // Do you mean you pass the 'answerr.Roman' in the function call
  118. //then you can use it here?
  119.  
  120. >answer=answer-1000;
  121. >i++;
  122. >}
  123. >while(answer-500>=0)
  124. >{
  125. >answerr.Roman[i]='D';
  126.   ^^^
  127. >answer=answer-500;
  128. >i++;
  129. >}
  130. >while(answer-100>=0)
  131. >{
  132. >answerr.Roman[i]='C';
  133.   ^^^
  134. >answer=answer-100;
  135. >i++;
  136. >}
  137. >while(answer-50>=0)
  138. >{
  139. >answerr.Roman[i]='L';
  140.    ^^^
  141. >answer=answer-50;
  142. >i++;
  143. >}
  144. >while(answer-10>=0)
  145. >{
  146. >answerr.Roman[i]='X';
  147.   ^^^
  148. >answer=answer-10;
  149. >i++;
  150. >}
  151. >while(answer-5>=0)
  152. >{
  153. >answerr.Roman[i]='V';
  154.   ^^^
  155. >answer=answer-5;
  156. >i++;
  157. >}
  158. >while(answer-1>=0)
  159. >{
  160. >answerr.Roman[i]='I';
  161.   ^^^
  162. >answer=answer-1;
  163. >i++;
  164. >}
  165. >}
  166. >
  167.  
  168. /* There are some important points
  169.  
  170.     1. none static member function has a hidden *this* pointer. That is
  171.          this->Roman[i] .... so and so ...( not recommented )
  172.       or just
  173.          Roman[i] .... so and so ... ( better )
  174.     then you do not need passing a char[] to access the Roman char array    
  175.  
  176.     2. Passing a variable  in a function call *does not* mean you can use 
  177.     that variable's name in the function definition body. 
  178.   i.e.
  179.    you call the function and pass 'answerr.Roman' as a parameter
  180.         DecimalToRoman(answerr.Roman,answer);
  181.    and then you want to use 'answerr.Roman' name in side the function    body
  182.     this is a BIG MISTAKE
  183.  
  184.     3. You obmiss the parameter name in the function definition. Is that 
  185.     your intense not to use that parameter? If I'm right, you *must* give it a 
  186.     name and then use that name in side the function definition instead of 
  187.     'answerr.Roman' or better yet throw it away and get familiar with 'this*'-see #1.
  188.  
  189. //Please review on your note or textbook
  190.   function prototype and function definition, they have 
  191.         different meaning and different syntax
  192.   
  193.   function calling and parameters passing
  194.   member function and this pointer .
  195. */
  196. Hoang Tuyen Q.
  197. HoangTQ@gnn.com
  198.  
  199.